/**
 * Created by TomKK on 2016/11/1.
 */
var request = require('request');
const cheerio = require('cheerio');
const iconv = require('iconv-lite');
const Bufferhelper = require('bufferhelper');
const fs = require('fs');
var readline = require('readline');

//字符串取出空格和回车
String.prototype.trim = function(){
    return this.replace(/\s/g,"");
}
// 创建readline接口实例
var  rl = readline.createInterface({
    input:process.stdin,
    output:process.stdout
});
var params = {
    "__VIEWSTATE":"",
    "txtUserName":"2014210972",
    "TextBox2":"***********",
    "txtSecretCode":"",
    "RadioButtonList1":"",
    "Button1":"",
    "lbLanguage":"",
    "hidPdrs":"",
    "hidsc":""
}
var baseUrl = "http://172.31.218.149/";
function transfer(html){
    var buffer = new Bufferhelper();
    buffer.concat(html);
    return iconv.decode(buffer.toBuffer(), "GBK").toString();
}

//允许cookie,记住cookie将来使用
request = request.defaults({jar: true})
request.get(
    {
        url:'http://172.31.218.149/default2.aspx',
        encoding:null
    }
,function(err,res,body){
    if(err){
        console.error(err);
        return;
    }
        console.log('状态码为' + res.statusCode);
        body = transfer(body);
        var $ = cheerio.load(body);
        var codeBuffer = new Bufferhelper();
        params.__VIEWSTATE = $("input[name=__VIEWSTATE]").attr("value");
        //开始获取验证码
        request.get('http://172.31.218.149/CheckCode.aspx')
            // .pipe(fs.createWriteStream('code.png')
        .on('error',function(error){
            console.error(error);
            return;
        })
        .on('data',function(chunk){
            codeBuffer.concat(chunk);
        })
        .on('end',function(){
            var html = codeBuffer.toBuffer();
            fs.writeFile('code.jpg',html,function(err){
                if(err){
                    console.error(err);
                    return;
                }
                console.log("二维码获取成功");
                rl.question("请输入验证码:",function(answer){
                    params.txtSecretCode = answer;
                    request.post(
                        {
                            url:'http://172.31.218.149/default2.aspx',
                            encoding:null,
                            form:params,
                            followAllRedirects:true
                        }
                        ,function(err,res,body){
                            var html = transfer(body);
                            if(html.indexOf("汤凯凯")>0){
                                console.log("登录成功");
                            }else{
                                console.log("登录失败");
                                return;
                            }
                            // fs.writeFile('home.html', html);
                            var $ = cheerio.load(html);
                            var allCourseUrl = baseUrl + "xsxk.aspx?xh=2014210972&xm=%CC%C0%BF%AD%BF%AD&gnmkdm=N121102"
                            request.get({
                                url:allCourseUrl,
                                encoding:null,
                                followAllRedirects:true,
                                headers:{
                                    Referer:"http://172.31.218.149/xs_main.aspx?xh=2014210972"
                                }
                            },function(err,res,body){
                                if(err){
                                    console.error(err);
                                    return;
                                }
                                var courses  = transfer(body);
                                fs.writeFile('courses.html', courses);
                                console.log(transfer(body))
                            })
                    })
                })
            })
        })
})

